<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseCommentBase extends Base {
	var $table = '';
	var $prefix = '';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $format = true;
	var $table_target = '';
	var $table_target_key = '';
	var $table_target_field = '';
	
	var $table_target_to_category = '';
	
	//
	var $target = 0;
	
	//constructor
	function BaseCommentBase() {
		parent::Base();
	}
	
	function setTarget($target) {
		$this->target = $target;
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$this->query_category = 0;
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		$query_str .= " order by c.comments_id desc ";
		
		if( $this->query_category == 1 ) {
			$query = "select c.*, t.* " 
					. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
					. " left join " . $this->table_target_to_category . " t2c on t." . $this->table_target_key . " = t2c." . $this->table_target_key . " "
					. " where 1 " . $query_str . $query_limit_str;
		} else {
			$query = "select c.*, t.* " 
					. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
					. " where 1 " . $query_str . $query_limit_str;
		}
		
		return $this->adb->getRows($query);
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$this->query_category = 0;
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		if( $this->query_category == 1 ) {
			$query = "select count(*) as count from " . $this->table . " c " 
					. " left join " . $this->table_target_to_category . " t2c on c.comments_target = t2c." . $this->table_target_key . " "
					. " where 1 " . $query_str;
		} else {
			$query = "select count(*) as count from " . $this->table . " c where 1 " . $query_str;
		}
		
		return $this->adb->getCount($query);
	}
	
	function getLatestItems($filter = '', $check = 0, $limit_length = 5) {
		if( $limit_length > 0 ) {
			//
		} else {
			$limit_lenght = 5;
		}
		$query_limit_str = " limit " . $limit_length;
		
		$this->query_category = 0;
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		if( $this->query_category == 1 ) {
			$query = "select * from " . $this->table . " c " 
					. " left join " . $this->table_target_to_category . " t2c on c.comments_target = t2c." . $this->table_target_key . " "
					. " where 1 " . $query_str . " order by c.comments_date_added desc " . $query_limit_str;
		} else {
			$query = "select * from " . $this->table . " c where 1 " . $query_str . " order by c.comments_date_added desc " . $query_limit_str;
		}
		
		return $this->adb->getRows($query);
	}
	
	//Item
	function checkItem($value, $type = 'id', $check = 0, $target = 0) {
		$value = func_db_input($value);
		
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and c.comments_status = 1";
		}
		if( $target > 0 ) {
			$query = "select count(*) as count " 
					. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
					. " where c.comments_id = '" . (int)$value . "' " . $query_str;
		} else {
			$query = "select count(*) as count from " . $this->table . " c where c.comments_id = '" . (int)$value . "' " . $query_str;
		}
		
		return $this->adb->checkCount($query);
	}
	
	function getItem($value, $type = 'id', $check = 0, $target = 0) {
		$value = func_db_input($value);
		
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and c.comments_status = 1";
		}
		if( $target > 0 ) {
			$query = "select c.*, t.* " 
					. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
					. " where c.comments_id = '" . (int)$value . "' " . $query_str;
		} else {
			$query = "select c.* from " . $this->table . " c where c.comments_id = '" . (int)$value . "' " . $query_str;
		}
		
		return $this->adb->getRow($query);
	}
	
	function getPreviousItem($value, $type = 'id', $check = 0, $target_id = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and c.comments_status = 1";
		}
		if( $target_id > 0 ) {
			$query_str .= " and c.comments_target = '" . (int)$target_id . "'";
		}
		$query_str .= " order by c.comments_id desc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item['comments_id'];
		}
		$query = "select c.*, t.* " 
				. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
				. " where c.comments_id < '" . (int)$value . "' " . $query_str . $query_limit_str;
		
		return $this->adb->getRow($query);
	}
	
	function getNextItem($value, $type = 'id', $check = 0, $target_id = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and c.comments_status = 1";
		}
		if( $target_id > 0 ) {
			$query_str .= " and c.comments_target = '" . (int)$target_id . "'";
		}
		$query_str .= " order by c.comments_id asc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item['comments_id'];
		}
		$query = "select c.*, t.* " 
				. " from " . $this->table . " c left join " . $this->table_target . " t on c.comments_target = t." . $this->table_target_key . " " 
				. " where c.comments_id > '" . (int)$value . "' " . $query_str . $query_limit_str;
		
		return $this->adb->getRow($query);
	}
	
	function addItem($data_array, $target_id = 0) {
		if( $target_id > 0 ) {
			$data_array['comments_target'] = $target_id;
		}
		if( $data_array['comments_target'] == 0 ) {
			return false;
		}
		$data_array = func_db_input($data_array);
		$data_array['comments_date_added'] = 'now()';
		$data_array['comments_added_by'] = $this->_get_current_user();
		$data_array['comments_status'] = '1';
		
		$items_id = $this->_insert($this->table, $data_array);
		
		//
		if( $this->table_target_field != '' ) {
			$query = "update " . $this->table_target . " set " . $this->table_target_field . " = " . $this->table_target_field . " + 1 " 
					. " where " . $this->table_target_key . " = '" . (int)$data_array['comments_target'] . "'";
			$result = $this->adb->query($query);
		}
		
		return $items_id;
	}
	
	function updateItem($items_id, $data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array['comments_modified_by'] = 'now()';
		$data_array['comments_modified_by'] = $this->_get_current_user();
		
		return $this->_update($this->table, $data_array, "comments_id = '" . (int)$items_id . "'");
	}
	
	function updateItems($items_ids, $data_array) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->_update($this->table, $data_array, "comments_id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->_update($this->table, $data_array, "comments_id in (" . $query_ids . ")");
			}
		}
	}
	
	function deleteItem($items_id) {
		$query = "delete " . $this->table . " from " . $this->table . " where comments_id = '" . (int)$items_id . "'";
		
		return $this->adb->query($query);
	}
	
	function deleteItems($items_ids) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->adb->deleteA($this->table, "comments_id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->adb->deleteA($this->table, "comments_id in (" . $query_ids . ")");
			}
		}
	}
	
	//target
	function addTargetItem($target_id, $data_array) {
		$data_array['comments_target'] = $target_id;
		
		return $this->addItem($data_array);
	}
	
	function getTargetItem($target_id, $items_id = '') {
		$query = "select * from " . $this->table . " where comments_target = '" . (int)$target_id . "'";
		if( $items_id != '' ) {
			$query .= " and comments_id = '" . (int)$items_id . "'";
		}
		
		return $this->adb->getRow($query);
	}
	
	function getTargetItems($target_id, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' ) {
			$filter = array();
			$filter['target'] = $target_id;
		} elseif( is_array($filter) ) {
			$filter['target'] = $target_id;
		}
		
		return $this->getItems($filter, $check, $limit);
	}
	
	function getTargetPrevious($target_id, $items_id) {
		$query = "select * from " . $this->table . " where comments_target = '" . (int)$target_id . "' and comments_id < '" . $items_id . "' " 
				. " order by comments_id desc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetNext($target_id, $items_id) {
		$query = "select * from " . $this->table . " where comments_target = '" . (int)$target_id . "' and comments_id > '" . $items_id . "' " 
				. " order by comments_id asc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetFirst($target_id) {
		$query = "select * from " . $this->table . " where comments_target = '" . (int)$target_id . "' order by comments_id asc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetLast($target_id) {
		$query = "select * from " . $this->table . " where comments_target = '" . (int)$target_id . "' order by comments_id desc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetStats($target_id) {
		$count = $this->getItemsTotal(array('tid' => $target_id));
		$first = $this->getTargetFirst($target_id);
		$last = $this->getTargetLast($target_id);
		
		$stats = array('count' => $count, 'first' => $first, 'last' => $last);
		
		return $stats;
	}
	
	function getTargetTotal($target_id) {
		return $this->adb->getCountA($this->table, "comments_target = '" . (int)$target_id . "'");
	}
	
	function updateTargetTotal($target_id) {
		if( $this->table_target_field != '' ) {
			$query = "update " . $this->table_target . " set " . $this->table_target_field . " = " . $this->table_target_field . " + 1 " 
					. " where " . $this->table_target_key . " = '" . (int)$target_id . "'";
			return $this->adb->update($query);
		}
	}
	
	function deleteTargetItems($target_id) {
		$query = "delete " . $this->table . " from " . $this->table . " where comments_target = '" . (int)$target_id . "'";
		
		return $this->adb->query($query);
	}
	
	function deleteTargetsItems($target_ids) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->adb->deleteA($this->table, "comments_target = '" . (int)$target_ids . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($target_ids)) . "'";
				$this->adb->deleteA($this->table, "comments_target in (" . $query_ids . ")");
			}
		}
	}
	
	//private
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and c.comments_status = 1";
		}
		if( $this->target > 0 ) {
			$query_str .= " and c.comments_target = '" . (int)$this->target . "' ";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and c.comments_subject like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['type']) ) {
				$query_str .= " and c.comments_type = '" . (int)$filter['type'] . "' ";
			}
			if( isset($filter['ids']) ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and c.comments_id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and c.comments_id = '" . (int)$filter['ids'] . "' ";
				}
			}
			if( isset($filter['category']) ) {
				$this->query_category = 1;
				if( is_array($filter['category']) ) {
					$query_str .= " and c.comments_target = t2c." . $this->table_target_key . " " 
								. " and t2c.categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and c.comments_target = t2c." . $this->table_target_key . " and t2c.categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
			if( isset($filter['target']) ) {
				$query_str .= " and c.comments_target = '" . (int)$filter['target'] . "' ";
			}
			if( isset($filter['tid']) ) {
				$query_str .= " and c.comments_target = '" . (int)$filter['tid'] . "' ";
			}
			if( isset($filter['targets']) && $this->_not_null($filter['targets']) == true ) {
				if( is_array($filter['targets']) ) {
					$query_str .= " and c.comments_target in (" . $this->_implode(', ', $filter['targets']) . ") ";
				} else {
					$query_str .= " and c.comments_target = '" . (int)$filter['targets'] . "' ";
				}
			}
			if( isset($filter['tids']) && $this->_not_null($filter['tids']) == true ) {
				if( is_array($filter['tids']) ) {
					$query_str .= " and c.comments_target in (" . $this->_implode(', ', $filter['tids']) . ") ";
				} else {
					$query_str .= " and c.comments_target = '" . (int)$filter['tids'] . "' ";
				}
			}
			if( isset($filter['authorid']) && $this->_not_null($filter['authorid']) == true ) {
				$query_str .= " and c.comments_authorid = '" . $filter['authorid'] . "' ";
			}
			if( isset($filter['author']) && $this->_not_null($filter['author']) == true ) {
				$query_str .= " and c.comments_author = '" . $filter['author'] . "' ";
			}
		}
		
		return $query_str;
	}
}
//
?>